package com.demo.action;

import java.util.List;
import java.util.Scanner;

import org.omg.PortableServer.SERVANT_RETENTION_POLICY_ID;

import com.demo.Model.Users;
import com.demo.service.UsersServiceImpl;

public class Action {

	public static void save() {
		//实例化Service业务层类
				UsersServiceImpl service=new UsersServiceImpl();
				Scanner sc=new Scanner(System.in);
				System.out.print("id:");
				String id=sc.next();
				System.out.print("loginname:");
				String loginname=sc.next();
				int b=service.save(id, loginname);
				if(b>=0) {
					System.out.println("操作成功");
				}else {
					System.out.println("操作失败");
				}
				sc.close();
	}
	public static void delete(String id) {
		UsersServiceImpl service=new UsersServiceImpl();
		if(service.delete(id)>=0) {
			System.out.println("删除成功");
		}else {
			System.out.println("删除失败");
		}
	}
	public static void findById(String id) {
		UsersServiceImpl service=new UsersServiceImpl();	
		Users user=service.findById(id);
		System.out.println(user);
	}
	public static void findAll() {
		UsersServiceImpl service=new UsersServiceImpl();
		List<Users> list=service.findAll();
		for(Users u:list) {
			System.out.println(u);
		}
		System.out.println("请输入要处理的数据id和编辑项目（1为修改，2为删除）");

	}
	public static void update(String id) {
		UsersServiceImpl service=new UsersServiceImpl();
		Scanner sc=new Scanner(System.in);
		System.out.print("请输入修改的loginname:");
		String loginname=sc.next();
		System.out.print("请输入修改的password:");
		String password=sc.next();
		System.out.print("请输入修改的username:");
		String username=sc.next();
		int b=service.update(new Users(id, loginname, password, username));
		if(b>=0) {System.out.println("更新成功");}
		else {System.out.println("更新失败");}

	}
	public static void main(String[] args) {
		while(1==1) {
			findAll();
			Scanner sc=new Scanner(System.in);
			String id=sc.next();
			int num=sc.nextInt();
			if(1==num) {
				System.out.print("即将更改的数据是");
				findById(id);
				update(id);
			}else if(2==num){
				System.out.print("即将删除的数据是");
				findById(id);
				delete(id);
			}
			System.out.println("\n全部数据：");
		}
		
	}
}
